package com.tripspins.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.ListView;

import com.tripspins.app.adapter.PinListAdapter;
import com.tripspins.app.bo.Trip;
import com.tripspins.app.helper.DbHelper;
import com.tripspins.app.helper.TRIP_STATUS;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;


/**
 * Created by João.Rutkoski on 08/10/13.
 */
public class ListPinsActivity extends Activity {

    DbHelper dbHelper;
    Boolean isOnTheRoad;
    ListView listView;
    Trip trip;
    private UiLifecycleHelper uiHelper;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_pins);
        getActionBar().setDisplayShowTitleEnabled(false);
        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);
        dbHelper = new DbHelper(this);
        try{
            trip = savedInstanceState.getParcelable("trip");
        }catch(Exception e){}
        if (trip == null)
        {
            Intent myIntent = getIntent();
            trip = myIntent.getParcelableExtra("param");
        }
       // loadPinData();
    }
    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {

    }

  /*  public void loadPinData()
    {
        PinListAdapter adapter = new PinListAdapter(this,trip);
        listView = (ListView)findViewById(R.id.list_pins);
        isOnTheRoad = trip.getStatus() == TRIP_STATUS.RUNNING;

        try {
            listView.setAdapter(adapter);
        }catch(Exception e1){
            e1.printStackTrace();
            System.gc();
            try{
                listView.setAdapter(adapter);
            }catch (Exception e2){e2.printStackTrace();}
        }
       /* listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState == SCROLL_STATE_FLING){}
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });*/

    @Override
    protected void onRestart() {
        super.onStart();
       // loadPinData();
        invalidateOptionsMenu();
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.clear();
        if(isOnTheRoad)
            getMenuInflater().inflate(R.menu.menu_pins_on_the_road, menu);
        else
            getMenuInflater().inflate(R.menu.menu_pins_off_the_road, menu);

        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.clear();
        if(isOnTheRoad)
            getMenuInflater().inflate(R.menu.menu_pins_on_the_road, menu);
        else
            getMenuInflater().inflate(R.menu.menu_pins_off_the_road, menu);
        return true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("trip", trip);
        uiHelper.onSaveInstanceState(outState);
    }
    @Override
    protected void onResume() {
        super.onResume();
        uiHelper.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_view_as_map:
                this.finish();
                break;
        }


            return super.onOptionsItemSelected(item);
    }
}